﻿Imports ADOEntityTempLog
Imports ADOEntity
Partial Class Administrator_purgeCreateXml_frmPurgeCreateXmlFile
    Inherits System.Web.UI.Page
    Private Const strCheckName As String = "CreateXmlLog"

    Private strName As String

    Private strPURGE_TO As String
    Private strPURGE_TABLE As String
    Private intMonth As Integer
    Private strPURGE_DATE_NAME As String



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        strName = Request.QueryString("strName")
        Dim strSql As String = ""
        Dim clsDb As New clsDatabase
        Dim systemConfigEntity As New systemConfigEntities
        Dim PurgeEntity As New PurgeEntities

       

        If strName = strCheckName Then
            Try
                'ดึงค่า กี่เดือนที่ล้างข้อมูล
                Dim tblSYSTEMCONFIG = (From SYSTEMCONFIG In systemConfigEntity.SSYSTEMCONFIG Where SYSTEMCONFIG.CONFIGCODE = "C03" Select SYSTEMCONFIG.CONFIGNUM).ToList
                Dim intMonth As String = tblSYSTEMCONFIG(0)

                'ดึงค่า  table ที่ต้องการล้างข้อมูล
                Dim tblPurge = (From PurgeJob In PurgeEntity.SPURGEJOB).ToList

                strSql = "UPDATE " & System.Configuration.ConfigurationSettings.AppSettings("TableSPURGEJOB") & " set STATUS_PURGE = 0  "
                clsDb.QueryExecuteNonQuery(strSql)

                clsDb.TransStart()
                For intCount As Integer = 0 To tblPurge.Count - 1

                    strPURGE_TO = tblPurge(intCount).PURGE_TO
                    strPURGE_TABLE = tblPurge(intCount).PURGE_TABLE
                    strPURGE_DATE_NAME = tblPurge(intCount).PURGE_DATE_NAME

                    strSql = "insert into " & strPURGE_TO & "  value  (select * from " & strPURGE_TABLE & " where  MONTHS_BETWEEN( TRUNC(SYSDATE, 'MM')  , TRUNC(  " & strPURGE_DATE_NAME & " , 'MM')  )  > " & intMonth & " ) "
                    clsDb.QueryExecuteNonQuery(strSql)
                    strSql = "delete from " & strPURGE_TABLE & "  where  MONTHS_BETWEEN( TRUNC(SYSDATE, 'MM')  , TRUNC(" & strPURGE_DATE_NAME & " , 'MM')  )  > " & intMonth
                    clsDb.QueryExecuteNonQuery(strSql)
                    strSql = "update  " & System.Configuration.ConfigurationSettings.AppSettings("TableSPURGEJOB") & " set  LAST_PURGE_DATE =  sysdate where PURGE_TABLE =  '" & strPURGE_TABLE & "' "
                    clsDb.QueryExecuteNonQuery(strSql)

                Next
                clsDb.TransCommit()
            Catch ex As Exception
                clsDb.TransRollBack()
                Try
                    strSql = "UPDATE " & System.Configuration.ConfigurationSettings.AppSettings("TableSPURGEJOB") & " set STATUS_PURGE = 2   WHERE PURGE_TABLE = '" & strPURGE_TABLE & "' "
                    clsDb.QueryExecuteNonQuery(strSql)
                Catch ext As Exception

                End Try
            End Try






            Try
                Dim tempLogEntity As New tempLogEntities
                Dim tblArrLogLogin = (From tblLogLogin In tempLogEntity.TEMP_LOG_LOGIN Order By tblLogLogin.LOGINDATE Ascending).ToArray

                strPURGE_TABLE = "SLOGINLOG"

                If Not (tblArrLogLogin.Count = 0) Then
                    Dim xeLogLogin As XElement = _
                        <NewDataset>
                            <%= From tblLog_Login In tblArrLogLogin _
                                Select _
                                <sLoginLog>
                                    <LOGSEQ><%= tblLog_Login.LOGSEQ %></LOGSEQ>
                                    <USERNAME><%= tblLog_Login.USERNAME %></USERNAME>
                                    <LOGINDATE><%= tblLog_Login.LOGINDATE %></LOGINDATE>
                                    <LOGINTIME><%= tblLog_Login.LOGINTIME %></LOGINTIME>
                                    <LOGINTYPE><%= tblLog_Login.LOGINTYPE %></LOGINTYPE>
                                    <LASTIP><%= tblLog_Login.LASTIP %></LASTIP>
                                    <DEPTCODE><%= tblLog_Login.DEPTCODE %></DEPTCODE>
                                    <BRANCHCODE><%= tblLog_Login.BRANCHCODE %></BRANCHCODE>
                                    <ROLECODE><%= tblLog_Login.ROLECODE %></ROLECODE>
                                    <ZONECODE><%= tblLog_Login.ZONECODE %></ZONECODE>
                                    <USERRIGHT><%= tblLog_Login.USERRIGHT %>02</USERRIGHT>
                                    <EMPNAME><%= tblLog_Login.EMPNAME %></EMPNAME>
                                    <EMPLASTNAME><%= tblLog_Login.EMPLASTNAME %></EMPLASTNAME>
                                </sLoginLog>
                            %>

                        </NewDataset>

                    xeLogLogin.Save("D:\PurgeDATA\backup_Log\Log_Login\Log_Login_" & createDateTime(tblArrLogLogin(0).LOGINDATE) & ".xml")
                    tempLogEntity.ExecuteStoreCommand("delete " + System.Configuration.ConfigurationSettings.AppSettings("TableTEMP_LOG_LOGIN"))

                    strSql = "UPDATE " & System.Configuration.ConfigurationSettings.AppSettings("TableSPURGEJOB") & " set STATUS_PURGE = 1   WHERE PURGE_TABLE = '" & strPURGE_TABLE & "' "
                    clsDb.QueryExecuteNonQuery(strSql)

                End If

                Dim tblArrLogTran = (From tblLogTran In tempLogEntity.TEMP_LOG_TRANSACTION Order By tblLogTran.LOGDATE Ascending).ToArray

                strPURGE_TABLE = "LOG_TRANSACTION"

                If Not (tblArrLogTran.Count = 0) Then
                    Dim xeLogTran As XElement = _
                      <NewDataset>
                          <%= From tblLog_Tran In tblArrLogTran _
                              Select _
                              <LOG_TRANSACTION>
                                  <LOGTRAN><%= tblLog_Tran.LOGTRAN %></LOGTRAN>
                                  <LOGDATE><%= tblLog_Tran.LOGDATE %></LOGDATE>
                                  <UNIQUEID><%= tblLog_Tran.UNIQUEID %></UNIQUEID>
                                  <UPDATEBY><%= tblLog_Tran.UPDATEBY %></UPDATEBY>
                                  <TRANTYPE><%= tblLog_Tran.TRANTYPE %></TRANTYPE>
                                  <DESCRIPTION><%= tblLog_Tran.DESCRIPTION %></DESCRIPTION>
                                  <PROGRAMCODE><%= tblLog_Tran.PROGRAMCODE %></PROGRAMCODE>
                              </LOG_TRANSACTION>
                          %>

                      </NewDataset>

                    xeLogTran.Save("D:\PurgeDATA\backup_Log\Log_Transaction\Log_Transaction_" & createDateTime(tblArrLogTran(0).LOGDATE) & ".xml")

                    tempLogEntity.ExecuteStoreCommand("delete " + System.Configuration.ConfigurationSettings.AppSettings("TableTEMP_LOG_TRANSACTION"))
                    strSql = "UPDATE " & System.Configuration.ConfigurationSettings.AppSettings("TableSPURGEJOB") & " set STATUS_PURGE = 1   WHERE PURGE_TABLE = '" & strPURGE_TABLE & "' "
                    clsDb.QueryExecuteNonQuery(strSql)

                End If
               
            Catch ex As Exception
                strSql = "UPDATE " & System.Configuration.ConfigurationSettings.AppSettings("TableSPURGEJOB") & " set STATUS_PURGE = 3   WHERE PURGE_TABLE = '" & strPURGE_TABLE & "' "
                clsDb.QueryExecuteNonQuery(strSql)

            End Try
        End If

        System.Threading.Thread.Sleep("5000")

        Me.ClientScript.RegisterStartupScript(Me.GetType(), "CloseWindowScript", "closeWindow();", True)
    End Sub

    Private Function createDateTime(ByVal dteFirstDate As Date) As String
        Dim strDateTime As String = dteFirstDate.ToString("yyyyMM")
        Return strDateTime
    End Function
    Private Function createDateTime() As String
        Dim strDateTime As String = Date.Now.ToString("yyyyMMdd_HHmmss")
        Return strDateTime
    End Function

End Class
